package com.platform.servlet;


import com.platform.util.CheckUtil;
import com.platform.util.MessageUtil;
import org.dom4j.DocumentException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/15.
 */
public class WeChatServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String signature = req.getParameter("signature");
        String timestamp = req.getParameter("timestamp");
        String nonce = req.getParameter("nonce");
        String echostr = req.getParameter("echostr");

        PrintWriter out = resp.getWriter();
        if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
            out.print(echostr);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //将传来的xml解析成消息对象，再将消息对象转为xml传给微信后台
        PrintWriter out = resp.getWriter();
        try {
            Map<String, String> map = MessageUtil.xml2Map(req);
            String fromUserName = map.get("FromUserName");
            String toUserName = map.get("ToUserName");
            String msgType = map.get("MsgType");
            String content = map.get("Content");
            String message = null;
            if (MessageUtil.MSGTYPE_TEXT.equals(msgType)) {
                if ("1".equals(content)) {//关键字判断
                    message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.firstSubMenu());
                } else if ("2".equals(content)) {
                    message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.secondSubMenu());
                } else if ("?".equals(content) || "？".equals(content)) {//关键字判断
                    message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
                } else {//这一段我自己添加的，如果未输入关键字的情况 显示未处理
                    //System.out.println(content + "---cont");
                    message = MessageUtil.initText(toUserName, fromUserName, "您输入的" + content + "我无法接招!");
                }
            } else if (MessageUtil.MSGTYPE_EVENT.equals(msgType)) {
                //事件类型再去细化判断
                String eventType = map.get("Event");
                if (MessageUtil.EVENT_SUBSCRIBE.equals(eventType)) {
                    message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
                }
            } else {
                message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
            }
            out.print(message);
            // System.out.println(message);
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }

    }
}
